package Week7.J0529;

import java.util.PriorityQueue;
import java.util.Scanner;

public class 字符编码 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int[] hash = new int[300];
            char[] s = scanner.next().toCharArray();
            for (char ch : s) {
                hash[ch]++;
            }

            PriorityQueue<Integer> heap = new PriorityQueue<>();
            for (int i = 0; i < 300; i++) {
                if (hash[i] != 0) heap.offer(hash[i]);
            }

            int ret = 0;
            while (heap.size() > 1) {
                int t1 = heap.poll();
                int t2 = heap.poll();
                ret += t1 + t2;
                heap.offer(t1 + t2);
            }

            System.out.println(ret);
        }
    }
}
